package com.shqcjd.generalplatformprojectclient.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.generalplatformprojectclient.R;
import com.shqcjd.generalplatformprojectclient.model.pojo.GuidanceHistoryBean;
import com.shqcjd.generalplatformprojectclient.ui.adapter.GuidanceHistoryAdapter;
import com.shqcjd.generalplatformprojectclient.ui.base.BaseActivity;
import com.shqcjd.generalplatformprojectclient.ui.presenter.GuidanceHistoryPresenter;
import com.shqcjd.generalplatformprojectclient.ui.view.GuidanceHistoryView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

public class GuidanceHistoryActivity extends BaseActivity<GuidanceHistoryView,GuidanceHistoryPresenter> implements GuidanceHistoryView {
    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.ib_toolbar_right)
    ImageButton ibToolbarRight;
    @BindView(R.id.et_search_guidance)
    EditText etSearchGuidance;
    @BindView(R.id.rv_guidance_list)
    RecyclerView rvGuidanceList;
    @BindView(R.id.fl_no_record)
    FrameLayout flNoRecord;
    private GuidanceHistoryAdapter guidanceHistoryAdapter;
    private int eId;
    List<GuidanceHistoryBean> mList = new ArrayList<>();
    List<GuidanceHistoryBean> adapterList = new ArrayList<>();

    @Override
    protected GuidanceHistoryPresenter createPresenter() {
        return new GuidanceHistoryPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_guidance_history;
    }

    @Override
    public void initView() {
        tvToolbarCenter.setText("远程指导记录");

        guidanceHistoryAdapter = new GuidanceHistoryAdapter(null);
        rvGuidanceList.setLayoutManager(new LinearLayoutManager(this));
        DefaultItemAnimator animator = new DefaultItemAnimator();
        rvGuidanceList.setItemAnimator(animator);
        rvGuidanceList.setAdapter(guidanceHistoryAdapter);
        guidanceHistoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<GuidanceHistoryBean> mBean = adapter.getData();
                List<GuidanceHistoryBean.GuideVideoBean> mList = new ArrayList<>();
                if(mBean.get(position).guideVideo!=null){
                    mBean.get(position).guideVideo.filename = "指导视频";
                    mList.add(mBean.get(position).guideVideo);
                }
                mList.addAll(mBean.get(position).guideFiles);
                Intent it = new Intent(GuidanceHistoryActivity.this,GuidanceDetailsActivity.class);
                it.putExtra("gList", (Serializable) mList);
                it.putExtra("comment", mBean.get(position).comment);
                startActivity(it);
            }
        });

        etSearchGuidance.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                getSearchList(s.toString());
            }
        });
        mPresenter.getRefreshData(eId);
    }

    private void getSearchList(String str){
        adapterList.clear();
        for(int i=0; i<mList.size(); i++){
            if(mList.get(i).comment.contains(str)){
                adapterList.add(mList.get(i));
            }
        }
        guidanceHistoryAdapter.setNewData(adapterList);
    }


    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void init() {
        super.init();
        eId = getIntent().getIntExtra("eId", -1);
    }

    @Override
    public void getDataError(String message) {
        Toasty.info(this, message).show();
    }

    @Override
    public void getRefreshDataSuccess(List<GuidanceHistoryBean> data) {
        mList = data;
        flNoRecord.setVisibility(data.size() == 0 ? View.VISIBLE : View.GONE);
        guidanceHistoryAdapter.setNewData(data);
    }

    @Override
    public void getMoreDataSuccess(List<GuidanceHistoryBean> data) {
        if (data.size() != 0) {
            guidanceHistoryAdapter.addData(data);
            guidanceHistoryAdapter.loadMoreComplete();
        } else {
            guidanceHistoryAdapter.loadMoreEnd();
        }
    }

    @Override
    public void showProgress(String tipString) {
        showWaitingDialog(tipString);
    }

    @Override
    public void hideProgress() {
        hideWaitingDialog();
    }
}
